package br.urca.impl;

import java.util.List;

import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;

import br.urca.dao.DepartamentoDAO;
import br.urca.model.Departamento;

@Transactional
public class JPADepartamentoDAO implements DepartamentoDAO {
	
	private EntityManager manager;
	
	public JPADepartamentoDAO() {
		this(null);
	}
	
	@Inject
	public JPADepartamentoDAO(EntityManager manager) {
		this.manager = manager;
	}

	@Override
	public List<Departamento> todos() {
		return this.manager.createQuery("SELECT d FROM Departamento d", Departamento.class)
				.getResultList();
	}

	@Override
	public Departamento buscarPorId(String codigo) {
		return this.manager.createQuery("SELECT d FROM Departamento d "
				+ "WHERE d.id = :codigo", Departamento.class)
				.setParameter("codigo", codigo)
				.getSingleResult();
	}

}
